/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: service_provider.cpp
 *
 * Purpose: service endian base class implementation for 'COM' Functional
 *
 * Developer:
 *   wen.gu , 2020-06-15
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#define LOG_TAG "SRVP"
#include "panda/com/service_provider.h"
#include "panda/com/runtime/ipc_service_provider.h"

#include "panda/core/log.h"

namespace panda
{
namespace com
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/

#define MakeServiceRequestHandlePtr() std::make_shared<ServiceRequestHandle>()
/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/

using PandaErrc = panda::core::PandaErrc;
using Value = panda::core::Value;
using IpcServiceProvider = panda::com::runtime::IpcServiceProvider;
using IpcPacketData = panda::com::runtime::IpcPacketData;
using IpcServiceRequestHandlePtr = panda::com::runtime::IpcServiceRequestHandlePtr;

class ServiceProvider::impl
{
public:
    IpcServiceProvider* mService;
};

class ServiceRequestHandle
{
public:
    IpcServiceRequestHandlePtr irhdl = nullptr;

};
/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/

 /**
 * srvName: the name of service.
 * multiThreadCnt: the count of thread for process request.
 * mode: how to work for current service.
 *
 */
ServiceProvider::ServiceProvider(const std::string& srvName, uint32_t multiThreadCnt /*= 1*/, ServiceWorkMode mode /*= ServiceWorkMode::kLocalOnly*/)
    :mImpl(new impl)
{
    if (mode == ServiceWorkMode::kLocalOnly)
    { 
        mImpl->mService = new IpcServiceProvider(srvName, multiThreadCnt);
    }
    else
    {
        LOGE("unsupport this work mode(%d) for service at now\n", (int32_t)mode);
        assert(0);
    }
    
}
ServiceProvider::~ServiceProvider()
{
    if (mImpl->mService)
    {
        delete mImpl->mService;
    }
}


PandaErrc ServiceProvider::start()
{
    return mImpl->mService->start();
}

PandaErrc ServiceProvider::stop()
{
    return mImpl->mService->stop();
}


/** api for method */
PandaErrc ServiceProvider::registerMethod(const std::string& name, ServiceMethodHandler handler)
{
    if (name.empty() || !handler) return PandaErrc::BadParameter;
    return mImpl->mService->registerMethod(name, [handler](IpcServiceRequestHandlePtr rhdl, const std::string& method, const IpcPacketData& data)
        {
            Value param;

            if (data.payload && (data.size > 0))
            {
                panda::core::JsonParse((const char*)data.payload.get(), (const char*)data.payload.get() + data.size, param);
            }
            ServiceRequestHandlePtr srhdl = MakeServiceRequestHandlePtr();
            srhdl->irhdl = rhdl;
            handler(srhdl, method, param);

        });
}

PandaErrc ServiceProvider::sendResponse(ServiceRequestHandlePtr reqHdl, const Value& data)
{
    if (!reqHdl) return PandaErrc::BadParameter;

    std::string paramStr;
    if (panda::core::JsonWriteRaw(data, paramStr))
    {
        return mImpl->mService->sendResponse(reqHdl->irhdl, (const uint8_t*)paramStr.data(), paramStr.size());
    }
    
    return PandaErrc::BadParameter;
}

/** api for signal */
void ServiceProvider::emitSignal(const std::string& sigName, const Value& data)
{
    if (sigName.empty()) return ;

    std::string paramStr;
    if (panda::core::JsonWriteRaw(data, paramStr))
    {
        mImpl->mService->emitSignal(sigName, (const uint8_t*)paramStr.data(), paramStr.size());
    }
}

} /** namespace com */
} /** namespace panda */


